﻿#include "BaseSpectroMeter.h"
#include <fstream>
#include <QDebug>


BaseSpectroMeter::BaseSpectroMeter()
{
//    connect(&_timer,&QTimer::timeout,this,&MyTimerEvent);
}

void BaseSpectroMeter::SetOtherConfig(QJsonObject config)
{
    QString filePath=config["SpecPath"].toString();
    _deviceIndex=config["DeviceIndex"].toInt();
    _specData.Parase(filePath);
    _serial=config["Serial"].toString();
}

bool BaseSpectroMeter::EnumSpectro(QList<AvsInfo> &listAvsInfo)
{
    AvsInfo tempAvsInfo;

//    tempAvsInfo.friendname = "a0000";
//    tempAvsInfo.serial = "1610146U200";
//    tempAvsInfo.status = 1;
//    listAvsInfo.append(tempAvsInfo);

//    tempAvsInfo.friendname = "a0002";
//    tempAvsInfo.serial = "1610146U300";
//    tempAvsInfo.status = 1;
//    listAvsInfo.append(tempAvsInfo);

    tempAvsInfo.friendname = "a0003";
    tempAvsInfo.serial = "1610146U400";
    tempAvsInfo.status = 1;
    listAvsInfo.append(tempAvsInfo);
    qDebug()<<__FUNCTION__<<"tempAvsInfo.name="<<tempAvsInfo.friendname<<"tempAvsInfo.serial"<<tempAvsInfo.serial;

    return true;
}

bool BaseSpectroMeter::SetMeasureParam(AvsMeasPara param)
{
    _measParam=param;
    return true;
}

bool BaseSpectroMeter::GetMeasParam(AvsMeasPara &param)
{
    param=_measParam;
    return true;
}

const QList<double> BaseSpectroMeter::GetLambda()
{
    //使用缓存技术
    if(_lambda.isEmpty())
    {
        QList<double> xaxis=_specData.first().XAxis();
        _lambda.append(xaxis);
    }
    return _lambda;
}

SpectroInfoTag BaseSpectroMeter::GetSpectrumInfo()
{
    SpectroInfoTag ret(GetLambda());
    ret.serilID=_serial;
    return ret;
}

AvsMeasResult BaseSpectroMeter::GetResult()
{
    static int index=0;

    QList<double> yVal;
//    for(int i=0;i<_specData.lambdacount();i++)
//    {
//        double y=_specData.intensity(i+index*_specData.lambdacount());
//        yVal.append(y);
//    }

//    index++;
//    if(index==_specData.speccount())
//    {
//        index=0;
//    }

    QList<QPointF> val;
    QList<double> lambda=GetLambda();
    for(int i=0;i<lambda.count();i++)
    {
        double y=yVal.at(i+(_deviceIndex-1)*2047);
        val.append(QPointF(lambda.at(i),y));
    }
    AvsMeasResult ret;
    ret.resultData=val;
    ret.SerialId=_serial;
    return ret;
}

bool BaseSpectroMeter::StartMeasure()
{
    QTimer::singleShot(10, this,&BaseSpectroMeter::MyTimerEvent);
    return true;
}

bool BaseSpectroMeter::Stop()
{
    return true;
}

const double BaseSpectroMeter::GetTemperature()
{
    return 58.3;
}


QVector<QPointF> BaseSpectroMeter::GetPoints(const QList<double> &values)
{
    QList<double> measure=values;
    QVector<QPointF> points;
    for(int i=0;i<measure.count();i++)
    {
        points.append(QPointF(i,measure.at(i)));
    }
    return points;
}

void BaseSpectroMeter::MyTimerEvent()
{
    auto data=GetResult();
    emit OnDataReceive(data);
}

bool BaseSpectroMeter::OpenSpectro()
{
    return true;
}

bool BaseSpectroMeter::CloseSpectro()
{
    return true;
}

bool BaseSpectroMeter::SoftTrigger()
{
    return false;
}
